package com.code.service.impl;

import com.code.config.MyThreadLocal;
import com.code.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.HashSet;

@Service
public class UserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    private UserService userService;
    /**
     * 自定义认证
     * @param
     * @return
     * @throws UsernameNotFoundException
     */
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        MyThreadLocal.threadLocal.set(username);
        com.code.domain.User user=userService.findUserByUserName(username);
        // 可以在自己的实体类上去实现 UserDetails 也可以写了类实现，或者直接用默认的类
        //  因为在项目中id使用的特别多，所以我们这边把用户的身份用id来储存
        User loginUser = new User(user.getId().toString(), user.getPassword(), new HashSet<>());
        return loginUser;
    }
}
